//server
function ajaxUrl() {

  //var url = "https://api1-hst.moqilin.net/"; //测试服务器
  var url = "https://orange-bluetooth.moqilin.net/";
  // var url = 'http://basic.loc/';// 本机测试地址
  return url;
}
//get
function requestGet(url, params, method, message, success, fail) {
  console.log(params)
  // wx.showNavigationBarLoading()
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  wx.request({
    url: url,
    data: params,
    method: method,
    header: {
      'Content-Type': 'application/json',
      'device': 'wechatmini',
      'apicode': '0652853d5946e6c924d78231e0ed0832'
      //'content-type': 'application/x-www-form-urlencoded'
    },
    success: function(res) {
      //console.log(res.data)
      wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }

      if (res.statusCode == 200) {
        success(res.data)
      } else {
        fail()
      }
    },
    fail: function(res) {
      // wx.hideNavigationBarLoading()
      if (message != "") {
        wx.hideLoading()
      }
      fail()
    },
    complete: function(res) {},
  })
}
//post
function requestPost(url, params, method, message, success, fail) {
  console.log(params)
  // wx.showNavigationBarLoading()
  if (message != "") {
    wx.showLoading({
      title: message,
    })
  }
  wx.getStorage({
    key: 'token',
    fail: function(){
      console.log(arguments);
    },
    success: function(res) {
      console.log(res);
      wx.request({
        url: url,
        data: params,
        method: method,
        header: {
          //'Content-Type': 'application/json'
          'content-type': 'application/x-www-form-urlencoded',
          'device': 'wechatmini',
          'token': res.data,
        },
        success: function(res) {
          //console.log(res.data)
          // wx.hideNavigationBarLoading()
          if (message != "") {
            wx.hideLoading()
          }
          if (res.statusCode == 200) {
            success(res.data)
          } else {
            fail()
          }
        },
        fail: function(res) {
          wx.hideNavigationBarLoading()
          if (message != "") {
            wx.hideLoading()
          }
          fail()
        },
        complete: function(res) {

        },
      })
    },

  })
}
//转时间戳
function appTime(ordertime) {
  var date = new Date(ordertime * 1000);
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + '';
  return Y + M + D + h + m;
}
//上传文件
function upload_file(url, filePath, name, formData, success, fail) {
  console.log('a=' + filePath)
  wx.uploadFile({
    url: url,
    filePath: filePath,
    name: name,
    header: {
      'content-type': 'multipart/form-data'
    }, // 设置请求的 header
    formData: formData, // HTTP 请求中其他额外的 form data
    success: function(res) {
      console.log(res);
      if (res.statusCode == 200 && !res.data.result_code) {
        typeof success == "function" && success(res.data);
      } else {
        typeof fail == "function" && fail(res);
      }
    },
    fail: function(res) {
      console.log(res);
      typeof fail == "function" && fail(res);
    }
  })
}
var arrImage = [];
var arrayImg = [];
//多张图片上传
function uploadimg(data) {
  var that = this,
    i = data.i ? data.i : 0,
    success = data.success ? data.success : 0,
    fail = data.fail ? data.fail : 0;
  wx.uploadFile({
    url: data.url,
    filePath: data.path[i],
    name: 'Assets[image]', //这里根据自己的实际情况改
    formData: null,
    success: function(resp) {
      success++;
      //console.log(resp)
      console.log(JSON.parse(resp.data))
      var data = JSON.parse(resp.data)
      if (data.code == 0) {
        arrImage.push(data.data.src)
        wx.setStorageSync('arrImage', arrImage)
      }
      //console.log(i);
      //这里可能有BUG，失败也会执行这里,所以这里应该是后台返回过来的状态码为成功时，这里的success才+1
    },
    fail: (res) => {
      fail++;
      console.log('fail:' + i + "fail:" + fail);
    },
    complete: () => {
      //console.log(i);
      i++;
      if (i == data.path.length) { //当图片传完时，停止调用          
        //console.log('执行完毕');
        console.log('成功：' + success + " 失败：" + fail);
      } else { //若图片还没有传完，则继续调用函数
        //console.log(i);
        data.i = i;
        data.success = success;
        data.fail = fail;
        that.uploadimg(data);
      }
    }
  });
}
//时间范围
function timeRange(beginTime, endTime) {
  var strb = beginTime.split(":");
  if (strb.length != 2) {
    return false;
  }
  var stre = endTime.split(":");
  if (stre.length != 2) {
    return false;
  }
  var b = new Date();
  var e = new Date();
  var n = new Date();
  b.setHours(strb[0]);
  b.setMinutes(strb[1]);
  e.setHours(stre[0]);
  e.setMinutes(stre[1]);

  if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
    return true;
  } else {
    //console.log ("当前时间是：" + n.getHours () + ":" + n.getMinutes () + "，不在该时间范围内！");
    return false;
  }
}
//弹出错误showModal
function errorShowModal(status, info) {
  if (status == 0) {

  } else if (status == -1) {

  } else {
    wx.showModal({
      title: '提示',
      content: info,
      confirmColor: '#ff8400',
      showCancel: false
    })
  }
}
//动态设置标题
function setTitle(data) {
  wx.setNavigationBarTitle({
    title: data,
  })
}
//拨打电话
function makePhoneCall(data) {
  wx.makePhoneCall({
    phoneNumber: data,
  })
}
//跳转带返回
function navigateTo(data) {
  wx.navigateTo({
    url: data,
  })
}

module.exports = {
  ajaxUrl: ajaxUrl,
  requestGet: requestGet,
  requestPost: requestPost,
  appTime: appTime,
  upload_file: upload_file,
  uploadimg: uploadimg,
  timeRange: timeRange,
  errorShowModal: errorShowModal,
  setTitle: setTitle,
  makePhoneCall: makePhoneCall,
  navigateTo: navigateTo
}